//
//  OverLapImageLay.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2018/2/5.
//  Copyright © 2018年 2Bulu. All rights reserved.
//

#import "OverLapImageLay.h"

@implementation OverLapImageLay
{
    __weak OverLapImageLayRenderer *_imgLayRender;
}

@synthesize renderer = _imgLayRender;

-(instancetype)initWithImage:(UIImage *)image onMapRect:(MKMapRect)rect alpha:(float)a
{
    self = [super init];
    if(self)
    {
        _image = image;
        _boundingMapRect = rect;
        MKMapPoint p = MKMapPointMake(rect.origin.x+rect.size.width/2, rect.origin.y+rect.size.height/2);
        _coordinate = MKCoordinateForMapPoint(p);
        _alpha = a;
    }
    
    return self;
}


-(OverLapImageLayRenderer *)renderer
{
    OverLapImageLayRenderer *rdr = _imgLayRender;
    if(!rdr)
    {
        rdr = [[OverLapImageLayRenderer alloc]initWithOverlay:self];
        rdr.alpha = self.alpha;
        _imgLayRender = rdr;
    }
    return rdr;
}

- (BOOL)canReplaceMapContent
{
    return NO;
}

@end


@interface OverLapImageLayRenderer ()

@property (nonatomic, readonly) OverLapImageLay *imageLay;

@end

@implementation OverLapImageLayRenderer

-(OverLapImageLay *)imageLay
{
    return (OverLapImageLay *)self.overlay;
}

- (void)drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)context
{
   // if(!MKMapRectIntersectsRect(mapRect, self.overlay.boundingMapRect))
    //    return;

    CGRect mapRectCG = [self rectForMapRect:mapRect];
    
    UIImage *image = self.imageLay.image;
    CGImageRef fullImgRef = image.CGImage;
    
    CGSize imageFullSize = self.imageLay.image.size;
    MKMapRect imageFullRectMK = self.overlay.boundingMapRect;
    
    CGFloat kx = (mapRect.origin.x-imageFullRectMK.origin.x)/imageFullRectMK.size.width;
    CGFloat ky = (mapRect.origin.y-imageFullRectMK.origin.y)/imageFullRectMK.size.height;
    CGFloat kw = mapRect.size.width/imageFullRectMK.size.width;
    CGFloat kh = mapRect.size.height/imageFullRectMK.size.height;
    
    CGRect tileRectCG = CGRectMake(kx*imageFullSize.width, ky*imageFullSize.height, kw*imageFullSize.width, kh*imageFullSize.height);
    CGImageRef tileImagRef = CGImageCreateWithImageInRect(fullImgRef, tileRectCG);
    
    
    double ratio = 1;
    CGRect drawRectCG = mapRectCG;
    CGFloat yPlus = 0, xPlus = 0;
    if(tileRectCG.origin.x < 0)
    {
        ratio = fabs(tileRectCG.origin.x/tileRectCG.size.width);
        xPlus = ratio*drawRectCG.size.width;
        drawRectCG.origin.x += xPlus;
        drawRectCG.size.width -= xPlus;
        
        tileRectCG.size.width += tileRectCG.origin.x;
        tileRectCG.origin.x = 0;
    }
    if(tileRectCG.origin.y < 0)
    {
        ratio = fabs(tileRectCG.origin.y/tileRectCG.size.height);
        yPlus = ratio*drawRectCG.size.height;
        drawRectCG.origin.y += yPlus;
        drawRectCG.size.height -= yPlus;
        
        tileRectCG.size.height += tileRectCG.origin.y;
        tileRectCG.origin.y = 0;
    }
    
    if(tileRectCG.origin.x + tileRectCG.size.width > imageFullSize.width)
    {
        drawRectCG.size.width  = drawRectCG.size.width*(imageFullSize.width-tileRectCG.origin.x)/tileRectCG.size.width;
    }
    
    if(tileRectCG.origin.y + tileRectCG.size.height > imageFullSize.height)
    {
        drawRectCG.size.height  = drawRectCG.size.height*(imageFullSize.height-tileRectCG.origin.y)/tileRectCG.size.height;
    }
    
    
    
    CGContextSaveGState(context);
    
    CGContextTranslateCTM(context, drawRectCG.origin.x, drawRectCG.origin.y);
    CGContextTranslateCTM(context, 0, drawRectCG.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, -drawRectCG.origin.x, -drawRectCG.origin.y);
    CGContextDrawImage(context, drawRectCG, tileImagRef);
    
    CGContextRestoreGState(context);
    
    //--------------------debug code-----------------------------
    
    CGContextSaveGState(context);
    double k = CGContextConvertSizeToUserSpace(context, (CGSize){1,1}).width*[UIScreen mainScreen].scale;
    CGContextSetLineWidth(context, k);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetAlpha(context, 0.7);
    
    CGContextStrokeRect(context,mapRectCG);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextStrokeRect(context,[self rectForMapRect:imageFullRectMK]);
    
    CGContextRestoreGState(context);
}


@end


